<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button> -->
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('订单')">导出</a-button> -->
      <!-- <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
        @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"></j-super-query>
      <!-- <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->
    </div>

    <!-- table区域-begin -->
    <div>
      <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
    selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->

      <a-table ref="table" size="middle" :scroll="{ x: true }" bordered rowKey="id" :columns="columns"
        :dataSource="dataSource" :pagination="ipagination" :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="settlementSlot" slot-scope="text">
          <div v-if="text == 1">否</div>
          <div v-if="text == 2">是</div>
        </template>
        <template slot="orderDetailsListSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>

        <template slot="orderSlot" slot-scope="text">
          <div v-if="text == 'SUCCESS'" class="status">待接单</div>
          <div v-if="text == 'REFUND'" class="status">转入退款</div>
          <div v-if="text == 'NOTPAY'" class="status">未支付</div>
          <div v-if="text == 'CLOSED'" class="status">已关闭</div>
          <div v-if="text == 'REVOKED'" class="status">已撤销</div>
          <div v-if="text == 'USERPAYING'" class="status">用户支付中</div>
          <div v-if="text == 'PAYERROR'" class="status" style=" background-color: red;">支付失败</div>
          <div v-if="text == 'REFUND_SUCCESS'" class="status" style=" background-color: #F1CA33;">退款成功</div>
          <div v-if="text == 'REFUND_PROCESSING'" class="status">退款中</div>
          <div v-if="text == 'REFUND_ABNORMAL'" class="status" style=" background-color: red;">退款异常</div>
          <div v-if="text == 'REFUND_CLOSED'" class="status">退款关闭</div>
          <div v-if="text == '1'" style=" background-color: #409EFF;" class="status">服务中</div>
          <div v-if="text == '2'" style=" background-color: #67C23A;" class="status">已完成</div>
          <div v-if="text == '3'" style=" background-color: #626AEF;" class="status">待签收</div>
          <div v-if="text == '4'" style=" background-color: #F89898;" class="status">待退款</div>
          <div v-if="text == '5'" style=" background-color: red;" class="status">拒绝退款</div>
          <div v-if="text == '6'" class="status" style=" background-color: #F1CA33;">退款成功</div>
        </template>

        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
            style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>

        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
          <!-- <a @click="showModal(record, 1)" style="margin-left: 10px;" v-if="record.orderType == 'SUCCESS'">接单</a>
          <a @click="showModal(record, 2)" style="margin-left: 10px;color:blue;"
            v-if="record.orderType == 'SUCCESS'">拒单</a>
          <a @click="showModal(record, 3)" style="margin-left: 10px;" v-if="record.orderType == '1'">确定完成</a>
          <a @click="showModal(record, 4)" style="margin-left: 10px;color:blue;" v-if="record.orderType == '4'">确认退款</a>
          <a @click="showModal(record, 5)" style="margin-left: 10px;color:red;" v-if="record.orderType == '4'">拒绝退款</a> -->
          <!-- <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown> -->
        </span>

      </a-table>
    </div>

    <machine-order-modal ref="modalForm" @ok="modalFormOk"></machine-order-modal>
    <a-modal v-model:open="open" title="更改订单状态" @ok="handleOk">
      <p v-if="orederState.orderType == 'SUCCESS' && orderTypeModel == '1'"> 此订单您确定要接单吗？</p>
      <p v-if="orederState.orderType == 'SUCCESS' && orderTypeModel == '2'"> 此订单您确定要拒绝接吗？</p>
      <p v-if="orederState.orderType == '1'"> 此订单您确定完成了吗？</p>
      <p v-if="orederState.orderType == '4' && orderTypeModel == '4'"> 此订单您确定要退款吗？</p>
      <p v-if="orederState.orderType == '4' && orderTypeModel == '5'"> 此订单您确定要拒绝退款吗？</p>
      <template #footer>
        <a-button key="back" @click="handleCancel">取消</a-button>
        <a-button key="submit" type="primary" :loading="loading" @click="handleOk">确认</a-button>
      </template>

    </a-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import MachineOrderModal from './modules/MachineOrderModal'

import { httpAction, getAction } from '@/api/manage'
export default {
  name: 'MachineOrderList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    MachineOrderModal
  },
  data() {
    return {
      description: '订单管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '订单号',
          align: "center",
          dataIndex: 'orderNum'
        },
        // {
        //   title: '商品名称',
        //   align: "center",
        //   dataIndex: 'goodName'
        // },
        {
          title: '订单状态',
          align: "center",
          dataIndex: 'orderType',
          scopedSlots: { customRender: 'orderSlot' }
        },
        {
          title: '定金金额',
          align: "center",
          dataIndex: 'paymentMoney'
        },
        {
          title: '总金额',
          align: "center",
          dataIndex: 'totalMoney'
        },
        {
          title: '支付时间',
          align: "center",
          dataIndex: 'paymentTime'
        },
        {
          title: '预计完成时间',
          align: "center",
          dataIndex: 'completeTime'
        },
        {
          title: '用户名称',
          align: "center",
          dataIndex: 'user.username',
        },
        {
          title: '用户手机号',
          align: "center",
          dataIndex: 'user.phone',
        },
        {
          title: '服务地址',
          align: "center",
          dataIndex: 'position',
        },
        {
          title: '服务类型',
          align: "center",
          customRender: (text, record) => {
            let str = ''
            record.orderDetailsList.map(item => {
              str += item.serviceName + ' ' + item.num + ' ' + (record.typeService == '2' ? item.unit : '小时') + ' '
            })
            return str // 自定义渲染逻辑
          },
          scopedSlots: { customRender: 'orderDetailsListSlot' }
        },
        {
          title: '是否结算',
          align: "center",
          dataIndex: 'settlement',
          scopedSlots: { customRender: 'settlementSlot' }
        },
        {
          title: '企业账号名',
          align: "center",
          dataIndex: 'userAccountUser.username',
        },

        {
          title: '备注',
          align: "center",
          dataIndex: 'notes'
        },

        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/order/machineOrder/list?type=2",
        delete: "/order/machineOrder/delete",
        deleteBatch: "/order/machineOrder/deleteBatch",
        exportXlsUrl: "/order/machineOrder/exportXls?type=2",
        importExcelUrl: "order/machineOrder/importExcel",

      },
      dictOptions: {},
      superFieldList: [],
      loading: false,
      open: false,
      orederState: {},
      orderTypeModel: 0,
    }
  },
  created() {
    this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = [];
      fieldList.push({ type: 'string', value: 'orderNum', text: '订单号', dictCode: '' })
      // fieldList.push({ type: 'string', value: 'goodId', text: '商品id', dictCode: '' })
      // fieldList.push({ type: 'string', value: 'goodName', text: '商品名称', dictCode: '' })
      // fieldList.push({ type: 'string', value: 'goodTable', text: '商品所属表名', dictCode: '' })
      fieldList.push({ type: 'string', value: 'orderType', text: '订单状态', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'paymentMoney', text: '定金金额', dictCode: '' })
      fieldList.push({ type: 'datetime', value: 'paymentTime', text: '支付时间' })
      // fieldList.push({ type: 'string', value: 'userAccountUser.username', text: '企业账号名', dictCode: '' })
      fieldList.push({ type: 'string', value: 'notes', text: '备注', dictCode: '' })
      this.superFieldList = fieldList
    },
    showModal(e, id) {
      this.open = true;
      this.orederState = e
      this.orderTypeModel = id
    },
    async handleOk() {
      this.loading = true;
      //接单
      if (this.orederState.orderType == 'SUCCESS' && this.orderTypeModel == '1') {
        this.orederState.orderType = '1'
        let userAccount = window.localStorage.getItem('pro__Login_Userinfo')
        let userid = JSON.parse(userAccount).value.id
        console.log(JSON.parse(userAccount).value.id);
        this.orederState.userAccountId = userid
        //订单id
        let orderReceive = { orderId: this.orederState.id }
        await httpAction('/order/machineOrder/edit', this.orederState, 'put').then((res) => {
          console.log(res);
          this.loadData() //刷新页面
          this.$message.success(res.message);
          this.loading = false;
          this.open = false;
        })
        // 订单接单短信接口
        await getAction('/sms/orderReceiving', orderReceive).then(res => {
          console.log(res);
          if (res.code == 200) {
            this.$message.success('成功发送短信');
          } else {
            this.$message.error('发送短信失败');
          }
        }).catch((err) => {
          this.$message.error(err)
        })
      } else if (this.orederState.orderType == 'SUCCESS' && this.orderTypeModel == '2') {
        //拒接接单
        // 退款
        let obj = {}
        // console.log("退款中", this.orederState);
        await httpAction(`/order/auth/refund?orderId=${this.orederState.id}`, obj, 'post').then(res => {
          console.log(res);
          this.$message.success(res.message);
          this.loadData() //刷新页面
          this.loading = false;
          this.open = false;
        })
      } else if (this.orederState.orderType == '1') {
        //完成订单
        this.orederState.orderType = '2'
        await httpAction('/order/machineOrder/edit', this.orederState, 'put').then((res) => {
          console.log(res);
          if (res.code == 200) {
            this.$message.success('确定成功');
          } else {
            this.$message.error('接口出错');
          }
          this.loading = false;
          this.open = false;
        })
        //订单id
        let orderReceive = { orderId: this.orederState.id }
        //订单完成订单短信接口
        await getAction('/sms/complete', orderReceive).then(res => {
          console.log(res);
          if (res.code == 200) {
            this.$message.success('成功发送短信');
          } else {
            this.$message.error('发送短信失败');
          }
        }).catch((err) => {
          this.$message.error(err)
        })
      } else if (this.orederState.orderType == '4' && this.orderTypeModel == '4') {
        // 退款
        let obj = {}
        console.log("退款中", this.orederState);
        await httpAction(`/order/auth/refund?orderId=${this.orederState.id}`, obj, 'post').then(res => {
          console.log(res);
          this.loadData() //刷新页面
          this.$message.success(res.message);
          this.loading = false;
          this.open = false;
        })
      } else if (this.orederState.orderType == '4' && this.orderTypeModel == '5') {
        // 拒绝退款
        this.orederState.orderType = '5'
        console.log("退款中", this.orederState);
        await httpAction('/order/machineOrder/edit', this.orederState, 'put').then((res) => {
          console.log(res);
          this.$message.success(res.message);
          this.loading = false;
          this.open = false;
        })
      }
    },
    handleCancel() {
      this.open = false;
    }

  }
}
</script>

<style scoped>
@import '~@assets/less/common.less';

.status {
  background-color: #969990;
  color: #fff;
  border-radius: 10px;
}
</style>